package com.lfg.leetcode.leetcode0;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class No90subsetsWithDup {

    public static void main(String[] args) {
        List<List<Integer>> ret = subsetsWithDup(new int[]{2, 1, 2, 1, 3});
//        List<List<Integer>> ret = subsetsWithDup(new int[]{1, 2, 2}, 2);
        System.out.println("[" + ret + "]");
    }

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> ret = new ArrayList<>();
        Arrays.sort(nums);
        subsetsWithDup(nums, 0, ret, new ArrayList<Integer>());
        return ret;
    }

    public static void subsetsWithDup(int[] nums, int index, List<List<Integer>> ret, List<Integer> data) {
        ret.add(new ArrayList<>(data));
        Integer last = null;
        for (int i = index; i < nums.length; i++) {
            if (last != null && nums[i] == last) {
                continue;
            }
            data.add(nums[i]);
            subsetsWithDup(nums, i + 1, ret, data);
            last = data.remove(data.size() - 1);
        }
    }
}

